<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@include file="../base.jsp" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<%@include file="../base_resource.jsp" %>
<link rel="stylesheet" type="text/css" href="${path}/js/jquery/easyui1.5/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="${path}/js/jquery/easyui1.5/themes/icon.css">
<script type="text/javascript" src="${path}/js/jquery-1.8.0.min.js"></script>
<script src="${path}/js/jquery/easyui1.5/jquery.easyui.min.js"></script>
<script src="${path}/js/jquery/easyui1.5/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="${path}/js/jquery.select.js"></script>


<script type="text/javascript">
	var companyId = 1;

	$(function(){
		treeShow();

		$("#win").dialog('close');

		$("#form_closeButton").click(function(){
			$("#win").dialog('close');
		});

		$("#form_addButton").click(function(){
			formAddEdit();
		});

	});

	function formAddEdit(){
		var formAddUrl = '${path}/admin123/user/orgAjax_addeditPostNode.action';
		$('#form_add').form({
			url:formAddUrl,//参数中有id,就是修改操作，否则是添加操作
			onSubmit: function(){
				return $(this).form('validate');
			},
			success:function(data){
				var checkJsonArr = /^\{(.+:.+,*)\}$/;
				var checkResult = checkJsonArr.test(data);
				if (!checkResult)
				{
					//普通字符串处理
					var er = data;
					window.parent.error(er);
				}
				else
				{
					//通过这种方法可将字符串转换为对象
					var data1 = jQuery.parseJSON(data);
					//success
					if(data1.responseCode == 1){
						$("#form_add").form("clear");
						$("#win").dialog('close');
						$("#tree").treegrid('reload');
					}else{
						$.messager.alert("操作结果",data1.responseContent);
					}
				}
			},
			error:function(er){
				window.parent.error(er);
			}
		});
		// submit the form
		$('#form_add').submit();
	}

	function treeShow(){
		$("#tree").treegrid({
			url:'orgAjax_queryPostJsonTreeByCompanyId.action',
			idField:'id',
			treeField:'text',
			rownumbers: true,
			animate:true,
			columns:[[
				{title:'ID',field:'id',width:30,hidden:true},
				{title:'岗位名称',field:'text',width:250},
				{field:'opt',title:'操作',width:250,align:'center',
					formatter:function(value,rowData,rowIndex){
						var type = rowData.type;//0:公司
						var rowData = JSON.stringify(rowData);
						rowData = encodeURI(rowData);
						rowData = encodeURI(rowData);
						var btn = "";
						btn += "<a class=\"addcls\" style=\"height:20px;\" onclick=\"addNode('"+rowData+"')\" href=\"javascript:void(0)\">添加</a> ";
						if(type != 0){
							btn += "<a class=\"editcls\" style=\"height:20px;\" onclick=\"modifyNode('"+rowData+"')\" href=\"javascript:void(0)\">修改</a> ";
							btn += "<a class=\"delcls\" style=\"height:20px;\" onclick=\"removeNode('"+rowData+"')\" href=\"javascript:void(0)\">删除</a> ";
						}
						return btn;
					}
				}
			]],
			onClickCell:function(field,row){
				if(field=='text'){
					//$(this).treegrid(row.state === 'closed' ? 'expand' : 'collapse', row.target);
					$(this).treegrid('toggle',row.id);
				}
			},
			onLoadSuccess:function(row, data){
				$('.addcls').linkbutton({plain:true,iconCls:'icon-add'});  //给每行后的button加easyui样式
				$('.editcls').linkbutton({plain:true,iconCls:'icon-edit'});  //给每行后的button加easyui样式
				$('.delcls').linkbutton({plain:true,iconCls:'icon-remove'});
			}

		});
	}

	function addNode(rowData){
		rowData = decodeURI(rowData);
		rowData = decodeURI(rowData);
		rowData = JSON.parse(rowData);
		//alert(rowData.id);
		var companyId = rowData.companyId;
		$("#win").dialog({'title':'添加框'});
		$("#win").dialog('open');
		$("#form_add").form('load',rowData);
		$("#form_add").form('load',{'optType':'add','name':''});
	}



	function modifyNode(rowData){
		rowData = decodeURI(rowData);
		rowData = decodeURI(rowData);
		rowData = JSON.parse(rowData);
		//alert(rowData.text);
		$("#win").dialog({'title':'编辑框'});
		$("#win").dialog('open');
		$("#form_add").form('load',rowData);
		$("#form_add").form('load',{'optType':'edit'});
	}

	/**
	* 删除node
	* @param rowData
	 */
	function removeNode(rowData){

		rowData = decodeURI(rowData);
		rowData = decodeURI(rowData);
		rowData = JSON.parse(rowData);
		var postId = rowData.id;

		confirm("删除后不可恢复，确认删除吗？",function(){
			$.ajax({
				type:"POST",
				url:'orgAjax_removePostNode.action',
				dataType:"json",
				data:{'companyId':companyId,'postId':postId},
				success:function(msg){
					if(msg.responseCode!=1) {
						alert(msg.responseContent);
					}
					$("#tree").treegrid('reload');
				},
				error:function(er){
					window.parent.error(er);
				}
			});
		})
	}

	function confirm(msg, callback) {
		$.messager.confirm("确认框", msg, function (r) {
			if (r) {
				callback();
			}
		});
		return false;
	}


</script>

</head>

<body class="font14">
	<div id="tree" style="margin: 20px;"></div>


	<!-- 添加弹出框 添加 -->
	<div id="win" class="easyui-dialog" title="添加修改数据框"　
		 style="display:none;width:300px;height:200px"
		 data-options="iconCls:'icon-save',modal:true,resizable:true"
		 buttons="#dlg-buttons">
		<form id="form_add" method="post" style="margin: 20px;">
			<input name="optType" type="hidden" class="easyui-textbox" data-options={"width":0}>
			<input name="id" type="hidden" class="easyui-textbox" data-options={"width":0}>
			<input name="companyId" type="hidden" class="easyui-textbox" data-options={"width":0}>
			<br/>
			岗位名称：<input name="name" class="easyui-textbox" data-options={"width":100}>
		</form>
	</div>

	<!-- 添加弹出框 Buttons -->
	<div id="dlg-buttons">
		<table cellpadding="0" cellspacing="0" style="width:100%">
			<tr>
				<td style="text-align:right">
					<a href="#" id="form_addButton" class="easyui-linkbutton" iconCls="icon-save">保存</a>
					<a href="#" id="form_closeButton" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
				</td>
			</tr>
		</table>
	</div>
</body>
</html>